package com.aku.controller;

import com.aku.entity.Door;
import com.aku.entity.Order;
import com.aku.service.DoorService;
import com.aku.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private DoorService doorService;

    @RequestMapping("/orderList")
    public String findAllDoor(Model model){
        List<Order> orders = orderService.findAllOrder();
        model.addAttribute("orders",orders);
        return "order_list";
    }

    @RequestMapping("/orderAdd")
    public String orderAdd(Order order){
        orderService.addOrder(order);
        return "redirect:/orderList";
    }

    @RequestMapping("/orderDelete")
    public String orderDelete(Integer id){
        orderService.removeOrderById(id);
        return "redirect:/orderList";
    }

    @RequestMapping("/orderInfo")
    public String orderInfo(Integer id,Model model){
        Order order = orderService.findOrderById(id);
        Door door = doorService.findDoorById(order.getDoor_id());
        model.addAttribute("order",order);
        model.addAttribute("doorName",door.getName());
        return "order_update";
    }

    @RequestMapping("/orderUpdate")
    public String orderUpdate(Order order){
        orderService.updateOrder(order);
        return "redirect:/orderList";
    }

}
